package qc.module.ehs.mapper;

import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsUnitTypeEnum;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.ehs.dto.hazard.QueryHazardByUnitResultItemDto;
import qc.module.ehs.dto.unit.EhsUnitDto;
import qc.module.ehs.dto.unit.EhsUnitSimpleDto;
import qc.module.ehs.dto.unit.EhsUnitTreeTableItemDto;
import qc.module.ehs.entity.EhsUnitBasic;

import java.util.List;

/**
 * EhsUnirBasicMapper
 *
 * @author QuCheng Tech
 * @create 2024/5/22
 */
@Mapper
public interface EhsUnitBasicMapper {
    EhsUnitBasicMapper MAPPER = Mappers.getMapper(EhsUnitBasicMapper.class);

    @Mapping(target = "unittype", source = "unittype.index")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "odr", source = "odr")
    EhsUnitDto toDto(EhsUnitBasic en);

    @Mapping(target = "unittype", source = "unittype.index")
    EhsUnitSimpleDto toDtoSimple(EhsUnitBasic en);

    @Mapping(target = "unittype", ignore = true)
    @Mapping(target = "flag", ignore = true)
    EhsUnitBasic toEntity(EhsUnitDto dto);

    List<EhsUnitDto> toDtoList(List<EhsUnitBasic> ens);
    
    List<EhsUnitSimpleDto> toDtoSimpleList(List<EhsUnitBasic> ens);

    @Mapping(target = "unittype", source = "unittype.index")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "children", ignore = true)
    @Named("toTreeTableItemDto")
    EhsUnitTreeTableItemDto toTreeTableItemDto(EhsUnitBasic en);

    @IterableMapping(qualifiedByName = "toTreeTableItemDto")
    List<EhsUnitTreeTableItemDto> toTreeTableItemDtoList(List<EhsUnitBasic> list);

    @Mapping(target = "unittype", source = "unittype.index")
    @Mapping(target = "children", ignore = true)
    @Named("toHazardByUnitResultItemTreeDto")
    QueryHazardByUnitResultItemDto toHazardByUnitResultItemTreeDto(EhsUnitBasic en);

    @IterableMapping(qualifiedByName = "toHazardByUnitResultItemTreeDto")
    List<QueryHazardByUnitResultItemDto> toHazardByUnitResultItemTreeDtoList(List<EhsUnitBasic> list);

    /**
     * 新增修改DTO转换为Entity，特殊处理
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2024/5/22
     */
    default EhsUnitBasic dtoToEntity(EhsUnitDto dto) {
        System.out.println("传入对象：" + dto);
        EhsUnitBasic en = EhsUnitBasicMapper.MAPPER.toEntity(dto);

        System.out.println("转换为entity：" + en);

        //处理组织类型，非空时进行转换，空值时使用默认
        Integer unittype = null;
        if (dto.getUnittype() != null && dto.getUnittype() > 0x0) {
            try {
                unittype = dto.getUnittype();
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setUnittype(getEhsUnitTypeEnum(unittype));
        
        Integer flag = null;
        if (dto.getFlag() != null) {
            try {
                flag = dto.getFlag();
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    /**
     * 根据枚举key获取枚举对象
     *
     * @param index
     * @return
     */
    static EhsUnitTypeEnum getEhsUnitTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsUnitTypeEnum item : EhsUnitTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return EhsUnitTypeEnum.AREA;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }
}
